//
//  MTDiscoverRateHeaderView.swift
//  MT
//
//  Created by ooops on 2020/5/14.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import MTCategory

protocol MTDiscoverRateHeaderViewDelegate: NSObjectProtocol {
    func didTapUserAvatar()
}

class MTDiscoverRateHeaderView: UICollectionReusableView {
    
    weak var delegate: MTDiscoverRateHeaderViewDelegate?
    
    class func reuseIdentity() -> String {
        return "MTDiscoverRateHeaderViewId"
    }
    
    
    var disableGesture: Bool = false {
        didSet {
            userAvatorView.isUserInteractionEnabled = !disableGesture
        }
    }
    
    var model: MTRankingInfoModel? {
        didSet {
            if let name = model?.nickname {
                userNickName.text = name
            }
            
            if let a = model?.area, let age = model?.age {
                userAreaLabel.text = "\(age)岁 | \(a)"
            }
            
            if let avator = model?.headerUrl {
                userAvatorView.mtSetImage(avator, nil)
            }
        }
    }
    
    var rateNumber: Int? {
        didSet {
            if let num = rateNumber {
                let iconName = num == 1 ? "rank-champion" : num == 2 ? "rank-champion-1" : num == 3 ? "rank-champion-2" : "rank-champion-star"
                let textColor: Int = num <= 3 ? 0xffc000 : 0xffffff
                let size = num == 1 ? CGSize(width: 88, height: 30) : CGSize(width: 72, height: 28)
                let font = PingFangSCSemiboldFont(num == 1 ? 20 : 16)
                //strengthLabel.attributedText = arrtibute(text: "No.\(num)", icon: iconName, position: 0, iconSize: CGSize(width: 16, height: 16), iconYFix: 0, textColor: textColor)
                rateIconView.image = UIImage(named: iconName)
                strengthLabel.text = "No.\(num)"
                strengthLabel.textColor = UIColor(hex: textColor)
                strengthLabel.font = font
                
                
                let corner = UIRectCorner(rawValue: UIRectCorner.topRight.rawValue | UIRectCorner.bottomRight.rawValue)
                let maskPath = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: size.width, height: size.height), byRoundingCorners: corner, cornerRadii: CGSize(width: 14, height: 14))
                let maskLayer = CAShapeLayer()
                maskLayer.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)
                maskLayer.path = maskPath.cgPath
                rateBgView.layer.mask = maskLayer
            }
        }
    }
    
    var discoverType: MTDiscoverTableFooterViewType?
    
    lazy var userAvatorView: UIImageView = {
        let imageV = UIImageView()
        imageV.layer.cornerRadius = 8
        imageV.layer.masksToBounds = true
        imageV.contentMode = .scaleAspectFill
        imageV.isUserInteractionEnabled = true
        return imageV
    }()
    
    lazy var rateBgView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor.text.x1d1a1a
        return v
    }()
    
    lazy var rateIconView: UIImageView = {
        let imageV = UIImageView()
        return imageV
    }()
    
    lazy var strengthLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCSemiboldFont(14)
        l.text = "实力榜"
        //l.textAlignment = .center
        l.preferredMaxLayoutWidth = 44
        l.adjustsFontSizeToFitWidth = true
        return l
    }()
    
    lazy var userNickName: UILabel = {
        let l = UILabel()
        l.font = PingFangSCMediumFont(16)
        l.textColor = .white
        l.text = "Marry"
        return l
    }()
    
    lazy var userAreaLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCMediumFont(12)
        l.textColor = .white
        l.text = "19岁 | 滨江区"
        return l
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        makeConstraint()
        let tap = UITapGestureRecognizer(target: self, action: #selector(MTDiscoverRateHeaderView.didTap))
        userAvatorView.addGestureRecognizer(tap)
    }
    
    @objc private func didTap() {
        self.delegate?.didTapUserAvatar()
    }
    
    private func setupUI() {
        addSubview(userAvatorView)
        addSubview(strengthLabel)
        addSubview(rateBgView)
        rateBgView.addSubview(rateIconView)
        rateBgView.addSubview(strengthLabel)
        addSubview(userNickName)
        addSubview(userAreaLabel)
    }
    
    private func makeConstraint() {
        userAvatorView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        rateBgView.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.equalToSuperview().offset(10)
            make.width.equalTo(88)
            make.height.equalTo(30)
        }
        
        rateIconView.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(4)
            make.width.equalTo(22)
            make.height.equalTo(24)
        }
        
        strengthLabel.snp.makeConstraints { (make) in
            make.left.equalTo(rateIconView.snp.right).offset(3)
            make.right.equalToSuperview().offset(-15)
            make.centerY.equalToSuperview().offset(-1)
        }
        
        userNickName.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(10)
            make.bottom.equalTo(userAreaLabel.snp.top).offset(5)
        }
        
        userAreaLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(10)
            make.bottom.equalToSuperview().offset(-10)
        }
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }


}
